//
//  HomeSearchServeTabVC.m
//  BusinessMarket
//
//  Created by Michael Li on 2018/11/26.
//  Copyright © 2018年 Michael Li. All rights reserved.
//

#import "HomeSearchServeTabVC.h"
#import "ServiceCell.h"
#import "ServiceModel.h"
#import "SearchConditionCell.h"
@interface HomeSearchServeTabVC ()<DZNEmptyDataSetSource, DZNEmptyDataSetDelegate,SearchConditionCellDelegate>
@property(nonatomic,strong) NSString *urlString;
@property(nonatomic,strong) AFHTTPSessionManager *manager;
@property(nonatomic,assign) NSInteger pageNum;
@property(nonatomic,assign) NSInteger pageSize;
@property(nonatomic,assign) NSInteger tag;
@property(nonatomic,strong) NSDictionary *dict;
@property(nonatomic,assign) NSInteger order;
@property(nonatomic,strong)  MJRefreshAutoNormalFooter *footer;
@end
@implementation HomeSearchServeTabVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.manager = [AFHTTPSessionManager manager];
    self.pageNum = 1;
    self.pageSize = 10;
    self.tableView.tableFooterView = [[UIView alloc]initWithFrame:CGRectZero];
}

- (void)setServeMuta:(NSMutableArray *)serveMuta
{
    _serveMuta = serveMuta;
    self.footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(reloadMore)];
    self.tableView.mj_footer = self.footer;
    [self.footer setTitle:[NSBundle bundleLocalizedStringForKey:@"footerTitle"] forState:MJRefreshStateNoMoreData];
    self.footer.stateLabel.font = [UIFont systemFontOfSize:12];
    self.footer.stateLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1];
    if (!serveMuta.count) {
        self.tableView.emptyDataSetSource = self;
        self.tableView.emptyDataSetDelegate = self;
    }else {
        if (serveMuta.count< 10) {
            self.footer.state = MJRefreshStateNoMoreData;
        }
    }
    [self.tableView reloadData];
}
- (void)setText:(NSString *)text
{
    _text = text;
}
#pragma mark-代理:时间,评价排序
- (void)searchConditionCell:(SearchConditionCell *)searchConditionCell withTag:(NSInteger )tag
{
    
    [WSProgressHUD setProgressHUDIndicatorStyle:WSProgressHUDIndicatorBigGray];
    [WSProgressHUD show];
    XLog(@"时间,评价排序:%ld",tag);
    
    self.order = tag-1;
    self.pageNum = 1;
    [self reloadInternetData];
}
-(void)reloadInternetData
{
    self.urlString = [NSString stringWithFormat:@"%@/association/indexSearch",kUrl];
    self.dict = @{
                  @"page":[NumberSwitchString numberSwitchString:self.pageNum],
                  @"rows":[NumberSwitchString numberSwitchString:self.pageSize],
                  @"param":self.text,
                  @"order":[NumberSwitchString numberSwitchString:self.order]
                  };
    XLog(@"self.dict:%@",self.dict);
    [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        XLog(@"reloadInternetData:%@",responseObject);
        NSArray *temp = [responseObject objectForKey:@"date"];
        self.serveMuta = [NSMutableArray array];
        if (temp.count) {
            for (NSDictionary *dict in temp) {
                ServiceModel *model =[ServiceModel mj_objectWithKeyValues:dict];
                [self.serveMuta addObject:model];
            }
            [self.tableView reloadData];
            if (temp.count< 10) {
                self.footer.state = MJRefreshStateNoMoreData;
            }
        }
        if (!self.serveMuta.count) {
            self.tableView.emptyDataSetSource = self;
            self.tableView.emptyDataSetDelegate = self;
        }
        [WSProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        XLog(@"error:%@",error);
    }];
}
-(void)reloadMore
{
    self.pageNum ++;
    if (self.tag == 0) {
        self.urlString = [NSString stringWithFormat:@"%@/association/indexSearch",kUrl];
        self.dict = @{
                      @"page":[NumberSwitchString numberSwitchString:self.pageNum],
                      @"rows":[NumberSwitchString numberSwitchString:self.pageSize],
                      @"param":self.text,
                      @"order":[NumberSwitchString numberSwitchString:self.order]
                      };
        XLog(@"self.dict:%@",self.dict);
        [self.manager POST:self.urlString parameters:self.dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            XLog(@"reloadMore:%@",responseObject);
            NSArray *temp = [responseObject objectForKey:@"date"];
            if (temp.count) {
                for (NSDictionary *dict in temp) {
                    ServiceModel *model =[ServiceModel mj_objectWithKeyValues:dict];
                    [self.serveMuta addObject:model];
                }
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }else {
                self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
            }
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            XLog(@"error:%@",error);
        }];
    }
}
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    UIImage *image = [UIImage imageNamed:@"empty"];
    return image;
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = [NSBundle bundleLocalizedStringForKey:@"noData"];
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont boldSystemFontOfSize:14],
                                 NSForegroundColorAttributeName:kLabel102Color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.serveMuta.count?self.serveMuta.count+1:0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    if (indexPath.row == 0) {
        SearchConditionCell *cell = [SearchConditionCell cellNibWithTableView:tableView];
        cell.delegate = self;
        return cell;
    }else {
        ServiceCell *cell = [ServiceCell cellNibWithTableView:tableView];
        ServiceModel *model = self.serveMuta[indexPath.row-1];
        cell.serviceModel = model;
        return cell;
        
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 44;
    }else {
        return 144;
        
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0) {
        if (self.returnValueBlock) {
            self.returnValueBlock(indexPath.row);
        };
    }
   
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}
@end
